﻿/* ==============================================================================
 * 功能描述：BattlePanel  
 * 
 * 创 建 者：郑镇泉
 * 
 * 创建日期：2016/4/26 21:04:40
 * ==============================================================================*/
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using ZZQFrameWork;

public class BattlePanel : BaseUI
{
    private BattleModule battleModule
    {
        get { return ModuleManager.Instance.Get<BattleModule>(); }
    }

    private BattlePanelMonsterInfo monsterInfo;
    private BattlePanelRoleSkillInfo roleSkillInfo;

    [HideInInspector]
    public MonsterVO currentMonster;

    [HideInInspector]
    public RoleDataVO currentRole;

    /// <summary>
    /// 逃跑按钮
    /// </summary>
    private Slider getAwaySlider;

    /// <summary>
    /// 是否处于逃跑当中
    /// </summary>
    private bool isAway = false;

    public override Enum_UIType GetUIType()
    {
        return Enum_UIType.BattlePanel;
    }

    protected override void OnAwake()
    {
        monsterInfo = GetComponentInChildren<BattlePanelMonsterInfo>();
        roleSkillInfo = GetComponentInChildren<BattlePanelRoleSkillInfo>();
        getAwaySlider = transform.GetComponentByPath<Slider>("RunAway");
        EventTriggerListener.Get(getAwaySlider.gameObject).SetEventHandle(Enum_TouchEventType.OnClick, OnGetAway);
    }

    protected override void OnOpen(params object[] objects)
    {
        currentMonster = battleModule.currentMonster.Data;
        currentRole = battleModule.currentPlayer.Data;
        monsterInfo.Data = currentMonster;
        roleSkillInfo.Data = currentRole;
        getAwaySlider.value = 1;
    }

    protected override void OnClose()
    {
        currentMonster = null;
        monsterInfo.Data = null;
    }

    protected override void OnPlayMusic()
    {
        SoundManager.Instance.PlayMusic("1003");
    }

    protected override void OnStopMusic()
    {
        SoundManager.Instance.PlayMusic("1002");
    }

    void OnEnable()
    {
        EventDispatcher.AddListener<int, int, int>(Events.MonsterEvent.OnUpdateHp, OnUpdateHp);
        EventDispatcher.AddListener<int>(Events.MonsterEvent.OnMonsterDeath, OnMonsterDeath);
    }


    void OnDisable()
    {
        EventDispatcher.RemoveListener<int, int, int>(Events.MonsterEvent.OnUpdateHp, OnUpdateHp);
        EventDispatcher.RemoveListener<int>(Events.MonsterEvent.OnMonsterDeath, OnMonsterDeath);
    }

    void OnUpdateHp(int uid,int hp,int maxHp)
    {
        monsterInfo.UpdateHp(hp,maxHp);
    }

    private void OnMonsterDeath(int uid)
    {
        Close();
    }

    /// <summary>
    /// 逃跑的时间计算id
    /// </summary>
    private uint getAwayTimeId = 0;

    /// <summary>
    /// 逃跑
    /// </summary>
    /// <param name="_listener"></param>
    /// <param name="_args"></param>
    /// <param name="_params"></param>
    private void OnGetAway(GameObject _listener, object _args, object[] _params)
    {
        if(isAway)return;
        isAway = true;

        getAwayTimeId = TimerHeap.AddTimer(0, 100, GetAway);
    }

    void GetAway()
    {
        getAwaySlider.value -= 0.01f;
        if (getAwaySlider.value < 0.02f)
        {
            TimerHeap.DelTimer(getAwayTimeId);
            Debug.Log("逃离成功");
            battleModule.GetAwayBattle();
        }
    }
}